package com.adong.tv.ui.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.adong.tv.data.ApiResult;
import com.adong.tv.data.MovieRepository;
import com.adong.tv.model.Movie;

/**
 * 详情页ViewModel
 */
public class DetailViewModel extends ViewModel {
    
    private final MovieRepository repository;
    private final MutableLiveData<Integer> movieId = new MutableLiveData<>();
    private LiveData<ApiResult<Movie>> movieDetail;
    
    public DetailViewModel() {
        repository = MovieRepository.getInstance();
    }
    
    /**
     * 获取电影详情
     * @param id 电影ID
     * @return 包含电影详情的LiveData
     */
    public LiveData<ApiResult<Movie>> getMovieDetail(int id) {
        // 如果ID不变，复用之前的请求结果
        if (movieId.getValue() != null && movieId.getValue() == id && movieDetail != null) {
            return movieDetail;
        }
        
        // 更新ID并获取新数据
        movieId.setValue(id);
        movieDetail = repository.getMovieDetail(id);
        return movieDetail;
    }
} 